/*
 * ParamParser.cpp
 *
 *  Created on: Apr 15, 2011
 *      Author: sms103
 */

#include "ParamParser.h"


ParamParser::ParamParser() {
	pthread_mutex_init(&paramMutex,NULL);
	paramMap = new map<string,string>();
}

/* Parameter file must contain one parameter per line. Whitespace is ignored.
 * A line beginning with a # is ignored.
 * Parameter lines are of the form: <paramName>  <paramValue>
 */
bool ParamParser::parseParams(const char * paramFileName){

	parFile.open(paramFileName,ifstream::in);
	if(parFile.fail()){
		cout << "No parameter file found, all defaults will be used."<<endl;
		return false;
	}

	string buf;
	istringstream *issBuf;
	string nameBuf;
	string valBuf;
	int lineNum = 0;

	while(getline(parFile,buf)){
		lineNum++;
		issBuf = new istringstream(buf);
		(*issBuf) >> nameBuf;
		(*issBuf) >> valBuf;

		//skip the line if it starts with # or it was blank
		if(nameBuf.size()==0 || nameBuf[0]=='#')continue;
		if(valBuf.size()==0){
			cout << "Error parsing parameter file on line "<<lineNum<<endl;
			parFile.close();
			return false;
		}
		paramMap->insert(pair<string,string>(nameBuf,valBuf));
		//cout <<nameBuf<<"="<<valBuf<<"\n";
		nameBuf.clear();valBuf.clear();
	}
	parFile.close();
	return true;
}

map<string,string> *ParamParser::getParamMap(){
	return paramMap;
}

//initializes a parameter of uint64 type given a pointer to the var, the
//key to the var in paramMap, and the default value
void ParamParser::setLongIntParam(uint64_t *paramVariable, string mapKey,
											uint64_t defaultVal){
	pthread_mutex_lock(&paramMutex);
	oss.str("");
	it = paramMap->find(mapKey);
	if(it != paramMap->end()){
		(*paramVariable) = strtoul(it->second.c_str(),NULL,0);
	}
	else{
		(*paramVariable) = defaultVal;//set it to default
		//insert the default into the paramMap
		oss<<(*paramVariable);
		paramMap->insert(pair<string,string>(mapKey,oss.str()));
	}
	pthread_mutex_unlock(&paramMutex);
}


//initializes a parameter of int type given a pointer to the var, the
//key to the var in paramMap, and the default value
void ParamParser::setIntParam(int *paramVariable, string mapKey,
											int defaultVal){
	pthread_mutex_lock(&paramMutex);
	oss.str("");
	it = paramMap->find(mapKey);
	if(it != paramMap->end()){
		(*paramVariable) = atoi(it->second.c_str());
	}
	else{
		(*paramVariable) = defaultVal;//set it to default
		//insert the default into the paramMap
		oss<<(*paramVariable);
		paramMap->insert(pair<string,string>(mapKey,oss.str()));
	}
	pthread_mutex_unlock(&paramMutex);
}


void ParamParser::setDoubleParam(double *paramVariable, string mapKey,double defaultVal){
	pthread_mutex_lock(&paramMutex);
	oss.str("");
	it = paramMap->find(mapKey);
	if(it != paramMap->end()){
		(*paramVariable) = strtod(it->second.c_str(),NULL);
	}
	else{
		(*paramVariable) = defaultVal;//set it to default
		//insert the default into the paramMap
		oss<<(*paramVariable);
		paramMap->insert(pair<string,string>(mapKey,oss.str()));
	}
	pthread_mutex_unlock(&paramMutex);
}

void ParamParser::setStringParam(string *paramVariable, string mapKey,string defaultVal){
	pthread_mutex_lock(&paramMutex);
	oss.str("");
	it = paramMap->find(mapKey);
	if(it != paramMap->end()){
		(*paramVariable) = it->second;
	}
	else{
		(*paramVariable) = defaultVal;//set it to default
		//insert the default into the paramMap

		paramMap->insert(pair<string,string>(mapKey,(*paramVariable)));
	}
	pthread_mutex_unlock(&paramMutex);
}

ParamParser::~ParamParser() {
	// TODO Auto-generated destructor stub
}
